/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.interaction3d.assembly.convert;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author micha
 */
public class Material
{

    public final String shader;
    public final Map<String, double[]> constants;
    public final Map<String, String> textures;

    public Material(String shader)
    {
        if(shader == null)
        {
            throw new NullPointerException();
        }

        this.shader = shader;
        this.constants = new HashMap<String, double[]>();
        this.textures = new HashMap<String, String>();
    }
    
    public void writeTo(OutputStream out) throws IOException
    {
        //Charset charset = Charset.defaultCharset();
        Charset charset = Charset.forName("UTF-8");

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset));
        //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
        try
        {
            writer.write("<?xml version=\"1.0\" encoding=\"" + charset.toString() + "\" ?>");
            writer.newLine();
            writeTo(writer);
        }
        finally
        {
            writer.close();
        }
    }

    private void writeTo(BufferedWriter writer) throws IOException
    {
        String xmls = "http://xml.interaction3d.org/assembly/Material";
        String xsi  = "http://www.w3.org/2001/XMLSchema-instance";
        String loc  = "http://xml.interaction3d.org/assembly Material.xsd";
        writer.write("<Material xmlns=\"" + xmls + "\" xmlns:xsi=\"" + xsi + "\" xsi:schemaLocation=\"" + loc + "\">");
        writer.newLine();

        writer.write("\t<Shader>" + shader +"</Shader>");
        writer.newLine();

        {
            writer.write("\t<Constants count=\"" + constants.size() + "\">");
            writer.newLine();

            for(Map.Entry<String, double[]> constant : constants.entrySet())
            {
                double[] values = constant.getValue();
                writer.write("\t\t<Constant name=\"" + constant.getKey() +"\" dimension=\"" + values.length +"\">");
                
                for(int i=0; i<values.length; i++)
                {
                    if(i>0) writer.write(" ");

                    writer.write(String.format("%f", values[i]));
                }

                writer.write("</Constant>");
                writer.newLine();
            }

            writer.write("\t</Constants>");
            writer.newLine();

            writer.write("\t<Textures count=\"" + textures.size() + "\">");
            writer.newLine();

            for(Map.Entry<String, String> constant : textures.entrySet())
            {
                writer.write("\t\t<Texture name=\"" + constant.getKey() +"\">");

                writer.write(constant.getValue());

                writer.write("</Texture>");
                writer.newLine();
            }

            writer.write("\t</Textures>");
            writer.newLine();

        }

        writer.write("</Material>");
    }

}
